package java0819;

public class ThreadDemo7 {
    private static final long count = 10_0000_0000L;

    //串行的来针对a和b进行自增
    public static void serial(){
        //System.currentTimeMillis()获取当前系统的毫秒级时间戳
        long beg = System.currentTimeMillis();
        int a = 0;
        for (long i = 0; i < count; i++){
            a++;
        }
        int b = 0;
        for (long i = 0; i < count; i++){
            b++;
        }
        long end = System.currentTimeMillis();
        System.out.println("time:" + (end - beg));
    }

    public static void concurrency(){
        long beg = System.currentTimeMillis();
        Thread t1 = new Thread(){
            @Override
            public void run() {
                int a = 0;
                for (long i = 0; i < count; i++){
                    a++;
                }
            }
        };
        t1.start();

        Thread t2 = new Thread(){
            @Override
            public void run() {
                int b = 0;
                for (long i = 0; i < count; i++){
                    b++;
                }
            }
        };
        t2.start();

        //需要保证t1和t2都执行完了以后，在结束及计时
        try {
            //join就是就是等待对应的线程结束
            //当t1和t2没执行完之前，join就会阻塞等待
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("time:" + (end - beg));
    }
    public static void main(String[] args) {
        serial();
        concurrency();

    }
}
